<template>
  <el-dialog
    :title="!dataForm.omsOrderId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="umsUserId">
      <el-input v-model="dataForm.umsUserId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="是否为团长" prop="omsIsHeader">
      <el-input v-model="dataForm.omsIsHeader" placeholder="是否为团长"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="pmsProductId">
      <el-input v-model="dataForm.pmsProductId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="团购商品id" prop="smsTgouId">
      <el-input v-model="dataForm.smsTgouId" placeholder="团购商品id"></el-input>
    </el-form-item>
    <el-form-item label="拼团id" prop="omsGroupId">
      <el-input v-model="dataForm.omsGroupId" placeholder="拼团id"></el-input>
    </el-form-item>
    <el-form-item label="团购商品名称" prop="pmsProductName">
      <el-input v-model="dataForm.pmsProductName" placeholder="团购商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品单价" prop="pmsProductPrice">
      <el-input v-model="dataForm.pmsProductPrice" placeholder="商品单价"></el-input>
    </el-form-item>
    <el-form-item label="团购商品团购价" prop="smsTgouPrice">
      <el-input v-model="dataForm.smsTgouPrice" placeholder="团购商品团购价"></el-input>
    </el-form-item>
    <el-form-item label="购买数量" prop="omsOrderNum">
      <el-input v-model="dataForm.omsOrderNum" placeholder="购买数量"></el-input>
    </el-form-item>
    <el-form-item label="订单创建时间" prop="omsCreateDate">
      <el-input v-model="dataForm.omsCreateDate" placeholder="订单创建时间"></el-input>
    </el-form-item>
    <el-form-item label="参团日期" prop="omsOrderAddDate">
      <el-input v-model="dataForm.omsOrderAddDate" placeholder="参团日期"></el-input>
    </el-form-item>
    <el-form-item label="付款时间" prop="omsOrderPayDate">
      <el-input v-model="dataForm.omsOrderPayDate" placeholder="付款时间"></el-input>
    </el-form-item>
    <el-form-item label="付款金额" prop="omsOrderPayMoney">
      <el-input v-model="dataForm.omsOrderPayMoney" placeholder="付款金额"></el-input>
    </el-form-item>
    <el-form-item label="状态 0:未进行 1.代付款 2.拼团中 3.拼团成功 4.拼团失败 " prop="omsOrderStatus">
      <el-input v-model="dataForm.omsOrderStatus" placeholder="状态 0:未进行 1.代付款 2.拼团中 3.拼团成功 4.拼团失败 "></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          omsOrderId: 0,
          umsUserId: '',
          omsIsHeader: '',
          pmsProductId: '',
          smsTgouId: '',
          omsGroupId: '',
          pmsProductName: '',
          pmsProductPrice: '',
          smsTgouPrice: '',
          omsOrderNum: '',
          omsCreateDate: '',
          omsOrderAddDate: '',
          omsOrderPayDate: '',
          omsOrderPayMoney: '',
          omsOrderStatus: ''
        },
        dataRule: {
          umsUserId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          omsIsHeader: [
            { required: true, message: '是否为团长不能为空', trigger: 'blur' }
          ],
          pmsProductId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          smsTgouId: [
            { required: true, message: '团购商品id不能为空', trigger: 'blur' }
          ],
          omsGroupId: [
            { required: true, message: '拼团id不能为空', trigger: 'blur' }
          ],
          pmsProductName: [
            { required: true, message: '团购商品名称不能为空', trigger: 'blur' }
          ],
          pmsProductPrice: [
            { required: true, message: '商品单价不能为空', trigger: 'blur' }
          ],
          smsTgouPrice: [
            { required: true, message: '团购商品团购价不能为空', trigger: 'blur' }
          ],
          omsOrderNum: [
            { required: true, message: '购买数量不能为空', trigger: 'blur' }
          ],
          omsCreateDate: [
            { required: true, message: '订单创建时间不能为空', trigger: 'blur' }
          ],
          omsOrderAddDate: [
            { required: true, message: '参团日期不能为空', trigger: 'blur' }
          ],
          omsOrderPayDate: [
            { required: true, message: '付款时间不能为空', trigger: 'blur' }
          ],
          omsOrderPayMoney: [
            { required: true, message: '付款金额不能为空', trigger: 'blur' }
          ],
          omsOrderStatus: [
            { required: true, message: '状态 0:未进行 1.代付款 2.拼团中 3.拼团成功 4.拼团失败 不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.omsOrderId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.omsOrderId) {
            this.$http({
              url: this.$http.adornUrl(`/order/order/info/${this.dataForm.omsOrderId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.umsUserId = data.order.umsUserId
                this.dataForm.omsIsHeader = data.order.omsIsHeader
                this.dataForm.pmsProductId = data.order.pmsProductId
                this.dataForm.smsTgouId = data.order.smsTgouId
                this.dataForm.omsGroupId = data.order.omsGroupId
                this.dataForm.pmsProductName = data.order.pmsProductName
                this.dataForm.pmsProductPrice = data.order.pmsProductPrice
                this.dataForm.smsTgouPrice = data.order.smsTgouPrice
                this.dataForm.omsOrderNum = data.order.omsOrderNum
                this.dataForm.omsCreateDate = data.order.omsCreateDate
                this.dataForm.omsOrderAddDate = data.order.omsOrderAddDate
                this.dataForm.omsOrderPayDate = data.order.omsOrderPayDate
                this.dataForm.omsOrderPayMoney = data.order.omsOrderPayMoney
                this.dataForm.omsOrderStatus = data.order.omsOrderStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/order/order/${!this.dataForm.omsOrderId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'omsOrderId': this.dataForm.omsOrderId || undefined,
                'umsUserId': this.dataForm.umsUserId,
                'omsIsHeader': this.dataForm.omsIsHeader,
                'pmsProductId': this.dataForm.pmsProductId,
                'smsTgouId': this.dataForm.smsTgouId,
                'omsGroupId': this.dataForm.omsGroupId,
                'pmsProductName': this.dataForm.pmsProductName,
                'pmsProductPrice': this.dataForm.pmsProductPrice,
                'smsTgouPrice': this.dataForm.smsTgouPrice,
                'omsOrderNum': this.dataForm.omsOrderNum,
                'omsCreateDate': this.dataForm.omsCreateDate,
                'omsOrderAddDate': this.dataForm.omsOrderAddDate,
                'omsOrderPayDate': this.dataForm.omsOrderPayDate,
                'omsOrderPayMoney': this.dataForm.omsOrderPayMoney,
                'omsOrderStatus': this.dataForm.omsOrderStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
